<%@ page import="com.kma.ncpractice2013.dao.UserGroupDAO" %>
<%@ page import="com.kma.ncpractice2013.model.UserGroup" %>
<%@ page import="com.kma.ncpractice2013.model.User" %>
<%@ page import="com.kma.ncpractice2013.model.Commission" %>
<%--
  User: Illyas
  Date: 11/10/13
  Time: 11:59 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset='utf-8'>
    <title>slider demo</title>
    <link rel='stylesheet' href='https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css'>
    <%--<link rel='stylesheet' href='jquery-ui.css'>--%>
    <style>#slider, #VIPSlider, #commissionSlider { display:inline-block; width:32%; }	</style>
    <script src='https://code.jquery.com/jquery-1.9.1.js' type="text/javascript"></script>
    <%--<script src='jquery-1.9.1.js'></script>--%>
    <script src='https://code.jquery.com/ui/1.10.3/jquery-ui.js' type="text/javascript"></script>
    <%--<script src='jquery-ui.js'></script>--%>

    <script>
        var minCommission = <% out.print(Commission.getMinValue()); %>;
        var maxCommission = <% out.print(Commission.getMaxValue()); %>;
        const initialCommission =
            <%
                out.print(Commission.getValue());
            %>;
        const initialDiscount =
                <%
                    UserGroupDAO dao = new UserGroupDAO();
                    UserGroup group = dao.getById(User.Group.ordinaryCustomer.getValue());
                    out.print(group.getDiscount());
                %>;
        const initialVIPDiscount =
                <%
                    group = dao.getById(User.Group.VIPCustomer.getValue());
                    out.print(group.getDiscount());
                %>;
        var discounts = {'#slider':'#nonVIPDiscount', '#VIPSlider':'#VIPDiscount'};

        $().ready(function()
        {
            $('#minCommission').html(minCommission);
            $('#maxCommission').html(maxCommission);
            $('#commission').html(initialCommission);
            $('#nonVIPDiscount').html(initialDiscount);
            $('#VIPDiscount').html(initialVIPDiscount);

            $.each(discounts,function(slider,discount)
            {
                $(slider).slider({ value: $(discount).html() });
            });
            $('#commissionSlider').slider({ value: $('#commission').html() });
            $.each(discounts,function(slider,discount)
            {
                $(slider).slider({
                    min: 0,
                    max: 33,
                    stop: function(event, ui)
                    {
                        $(discount).html($(slider).slider('values',0));
                    },
                    slide: function(event, ui)
                    {
                        $(discount).html($(slider).slider('values',0));
                    }
                });
            });
            $('#commissionSlider').slider({
                min: 3,
                max: 17,
                stop: function(event, ui)
                {
                    $('#commission').html($('#commissionSlider').slider('values',0));
                },
                slide: function(event, ui)
                {
                    $('#commission').html($('#commissionSlider').slider('values',1));
                }
            });
        });

        function saveDiscounts()
        {
            $.post('/ReservationSystem_WEB_war/manager',
                    {
                        action: 'saveDiscounts',
                        nonVIPDiscount : $('#nonVIPDiscount').html().trim(),
                        VIPDiscount : $('#VIPDiscount').html().trim()
                    },
                    function(data)
                    {
                        $('#discountMessage').html('Discount values saved');
                    });
        }

        function saveCommission()
        {
            $.post('/ReservationSystem_WEB_war/manager',
                    {
                        action: 'saveCommission',
                        commission : $('#commission').html().trim()
                    },
                    function(data)
                    {
                        $('#commissionMessage').html('Commission value saved');
                    });
        }
    </script>

</head>
<body>
    <span id='discountMessage'></span><br>
    Current discount for non-VIP customers:
    <span type='text' id='nonVIPDiscount'></span>%<br>
    0% <div id='slider'></div> 33%<br>
    Current discount for VIP customers:
    <span type='text' id='VIPDiscount'></span>%<br>
    0% <div id='VIPSlider'></div> 33%<br>
    <input type='button' value='Save discount rules' onclick='saveDiscounts()'>
    <br><br>
    <span id='commissionMessage'></span><br>
    Current agency commission:
    <span type='text' id='commission'></span>%<br>
    <span id="minCommission"></span>
    %<div id='commissionSlider'></div>
    <span id="maxCommission"></span>
    %<br>
    <input type='button' value='Save commission rules' onclick='saveCommission()'>

</body>

</html>